package mark.tom.sail;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.activity.ComponentActivity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

import mark.tom.sail.demo.databinding.ActivityMainBinding;

public class MainActivity extends ComponentActivity {

    private int count = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(activityMainBinding.getRoot());

        LogInterceptor logInterceptor = new LogInterceptor() {
            @Override
            public void log(@Nullable String message, int level) {
                Log.println(level, "MainActivity", message);
            }
        };
        Sail.newBuilder(getApplicationContext())
                .setConverter(new GsonDataConverter(logInterceptor))
                .setLogInterceptor(logInterceptor)
                .setRecordCollector(new WorkManagerCollector(getApplicationContext()))
                .setRecordUploader(new RecordUploader() {
                    @Override
                    public void upload(@NonNull String tag, @NonNull List<String> data, @Nullable UploadResult result) {
                        Log.i("MainActivity", data.toString());
                        if (null != result) {
                            result.success();
                        }
                    }
                })
                .build();

        activityMainBinding.test.setText("记录");
        activityMainBinding.test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Sail.collect(new AnalyticsRecord().put("clickAction", ++count));
            }
        });

        activityMainBinding.test1.setText("上传");
        activityMainBinding.test1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Sail.report();
            }
        });
    }
}
